package ibm.etp.shen;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import ibm.etp.bean.ReadEls;

import javax.servlet.http.HttpServletRequest;

import jxl.read.biff.BiffException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class ReadElsActions extends ActionSupport implements
		ServletRequestAware {

	public String upload;

	public String message;

	public HttpServletRequest request;

	private String uploadContentType;

	private String uploadFileName;

	private String savePath;

	public String getUpload() {
		return upload;
	}

	public void setUpload(String upload) {
		this.upload = upload;
	}

	public void setSavePath(String value) {
		this.savePath = value;
	}

	private String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String execute() {
		System.out.println("success");
		System.out.println(getUploadContentType());

		System.out.println(getUploadFileName());
		
		try {
			FileOutputStream fos = new FileOutputStream("D:\\eclipse\\eclipse\\Workspace" + "\\" + getUploadFileName());
			FileInputStream fis = new FileInputStream(getUpload());

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println(upload);

		try {
			ReadEls.read(new File("D:\\eclipse\\eclipse\\Workspace" + "\\"+ getUploadFileName()));
		} catch (BiffException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "success";
	}

}
